/*****************************************************************************************
   
   Copyright (c) Microsoft Corporation. All rights reserved.

   Use of this code sample is subject to the terms of the Microsoft
   Permissive License, a copy of which should always be distributed with
   this file.  You can also access a copy of this license agreement at:
   http://www.microsoft.com/resources/sharedsource/licensingbasics/permissivelicense.mspx

 ****************************************************************************************/

using System;

namespace GrooveWebServicesV12Helpers
{
    public class Application
    {
        #region Application Properties And Methods

        public static GrooveApplicationWebService.GrooveApplication CreateInitializedApplicationWebService(string i_IdentityURL, string i_TargetURI, GrooveWebServicesV12Helpers.Context i_Context)
        {
            //  Create the service
            GrooveApplicationWebService.GrooveApplication ApplicationWebService = new GrooveApplicationWebService.GrooveApplication();

            //  Create the Groove SOAP header
            ApplicationWebService.GrooveRequestHeaderValue = new GrooveApplicationWebService.GrooveRequestHeader();

            //  Set appropriate header values
            ApplicationWebService.GrooveRequestHeaderValue.GrooveRequestKey = i_Context.RequestKey;
            ApplicationWebService.GrooveRequestHeaderValue.GrooveIdentityURL = i_IdentityURL;

            //  Set the URL
            ApplicationWebService.Url = i_Context.Host + i_TargetURI;

            //  Set Timeout
            ApplicationWebService.Timeout = i_Context.Timeout;

            return ApplicationWebService;
        }

        /// <summary>
        /// Specifies how Groove should optimize network communications
        /// </summary>
        /// <param name="i_IdentityURI">URI of the current Identity</param>
		/// <param name="i_Engage">Set to true to optimize network communications for sharing resources with other applications, false to optimize network communications for the interactive Groove Web Services application user. </param>
        /// <param name="i_Context">Web Services Context</param>
        public static void SociableCommunications(string i_IdentityURI, bool i_Engage, GrooveWebServicesV12Helpers.Context i_Context)
        {
            //  Get initialized reference to web service
            GrooveApplicationWebService.GrooveApplication ApplicationWebService = GrooveWebServicesV12Helpers.Application.CreateInitializedApplicationWebService
                (
                i_IdentityURI,
                GrooveWebServicesV12Helpers.Shared.GROOVE_WEB_SERVICES_APPLICATION_URI,
                i_Context
                );

            System.DateTime BeginDateTime = System.DateTime.Now;

            if (i_Engage)
            {
                //  Ask service to engage sociable communictions
                ApplicationWebService.EngageSociableCommunications();
            }
            else
            {
                //  Ask service to disengage sociable communictions
                ApplicationWebService.DisengageSociableCommunications();
            }

            if (GrooveWebServicesV12Helpers.Context.TraceEnabled)
            {
                System.TimeSpan TimeSpan = System.DateTime.Now.Subtract(BeginDateTime);

                if (i_Engage)
                    System.Diagnostics.Debug.WriteLine("ApplicationWebService::EngageSociableCommunications, " + TimeSpan.TotalMilliseconds + " ms");
                else
                    System.Diagnostics.Debug.WriteLine("ApplicationWebService::DisengageSociableCommunications, " + TimeSpan.TotalMilliseconds + " ms");
            }
        }

        #endregion

    }
}
